package org.rbac.serv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.rbac.po.PermissionAction;
import org.rbac.po.PermissionAuthority;
import org.rbac.po.PermissionAuthorityResource;
import org.rbac.vo.Authority;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 权限管理
 * <pre>
 * 项目名称：rbac
 * 类名称：AuthorityServices
 * 类描述：权限管理
 * 创建人：唐家林(tjl)
 * 创建时间：2014年10月8日　下午3:43:32
 * 修改人：
 * 修改时间：
 * 修改备注：
 * @version
 * </pre>
 */
@Service
public class AuthorityServices extends CommonServices {

	/**
	 * 修改
	 * 
	 * @param a
	 *            修改的数据
	 * @throws Exception
	 */
	public void moidifyAuthority(Authority a) throws Exception {
		this.editAuthority(a);
	}
	
	/**
	 * 修改状态
	 * 
	 * @param ids
	 *            修改状态的数据主键
	 * @param status
	 *            状态(0:删除, 1:正常, 2:禁用)
	 * @throws Exception
	 */
	public void moidifyAuthorityStatus(Integer[] ids, short status) throws Exception {
		String hql = "UPDATE PermissionAuthority pa SET pa.AStatus = :status WHERE pa.id IN (:ids)";
		Query q = getDao().getSession().createQuery(hql);
		q.setParameter("status", status);
		q.setParameterList("ids", ids);
		q.executeUpdate();
	}

	/**
	 * 编辑(新增或修改)
	 * 
	 * @param a
	 *            操作的数据
	 */
	public void editAuthority(Authority a) {
		if (null == a) {
			return;
		}
		// 权限
		PermissionAuthority authority = null;
		if (null == a.getId()) {
			// 权限
			authority = new PermissionAuthority();
		} else {
			// 权限
			authority = (PermissionAuthority) getDao().get(PermissionAuthority.class, a.getId());
		}
		// 属性值拷贝(拷贝r对象的属性值到role对象中对应名称的属性,且不拷贝id和RCdate的值)
		BeanUtils
				.copyProperties(a, authority, new String[] { "id", "AStatus" });

		if (null == a.getId()) {
			short status = 1;
			authority.setAStatus(status);
			getDao().save(authority);

		} else {
			getDao().update(authority);
		}

		/*// 删除以前的关系
		String hql = "DELETE FROM PermissionAuthorityResource par WHERE par.permissionAuthority.id = ? ";
		getDao().executeHQL(hql, authority.getId());

		// 建立新的关系
		Integer[] resids = a.getResid();
		PermissionAction pa = null;
		List<PermissionAuthorityResource> pars = new ArrayList<PermissionAuthorityResource>();
		for (Integer resid : resids) {
			pa = (PermissionAction) getDao().get(PermissionAction.class, resid);
			pars.add(new PermissionAuthorityResource(null, authority, pa));
		}
		getDao().saveList(pars);*/
	}
	/**
	 * 资源绑定权限
	 * @param aid
	 * @param resid
	 */
	public void resBindAuth(Integer aid,Integer[] resid){
	    String hql="select new map(permissionAction.id as key,permissionAuthority.id as val) from PermissionAuthorityResource where permissionAuthority.id=:id";
	    Map<String, Object> args = new HashMap<String, Object>();
	    args.put("id", aid);
	    Map<String, Object> map = getDao().query(hql, args, "key", "val"); 
	    
        PermissionAction pa = null;
        PermissionAuthority authority = new PermissionAuthority();
        authority.setId(aid);
        List<PermissionAuthorityResource> pars = new ArrayList<PermissionAuthorityResource>();
        for (Integer res : resid) {
           // pa = (PermissionAction) getDao().get(PermissionAction.class, res);
            if (!map.containsKey(res)) {
                pa = new PermissionAction();
                pa.setId(res);
                pars.add(new PermissionAuthorityResource(null, authority, pa));
            }
        }
        getDao().saveList(pars);
	    
	    
	}
	/**
	 * 删除权限资源
	 * @param resid
	 */
	public void deleteAuthOfRes(Integer[] resid){
	    String hql="delete from PermissionAuthorityResource where id in(:id)";
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("id", resid);
	   int affected= getDao().executeHQL(hql, map);
	   System.out.println(affected);
	}
}
